﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommonFunc;
using SQLExpressDAL;
using SQLExpressDAL.UsersTableAdapters;
using System.Text;
using System.Data.SqlClient;

public partial class Manageweb_Member_ViewXiaXian : System.Web.UI.Page
{

    public string StrKey;
    DataView dv;
    DataTable dt;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
     
        StrKey = CheckData();
        if (hidMiYao.Value.Equals("1"))
        {            
             InitData();          
           
        }      
 
    }

    private void InitData()
    {
        this.TreeView1.Nodes.Clear();
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetAllUsers();
        dt = (DataTable)userTable;

        SQLExpressDAL.Users.UsersDataTable singleUserTable = userAdp.GetSingleUserInfo(Session["UserID"].ToString());
        DataRow row = singleUserTable.Rows[0];

        //创建根节点 
        TreeNode root = new TreeNode();
        //必须与数据库的对应 
        root.Text = string.Format(" [{0}] | {1} | {2} | {3}", row["userid"].ToString(), row["username"].ToString(), row["LjScore"].ToString(), row["SpecialScore"].ToString());

        root.Value = row["userid"].ToString();
        //添加根节点 
        this.TreeView1.Nodes.Add(root);
        //第一次加载时调用方法传参 
        CreateTree(Session["UserID"].ToString(), root, dt, this.TreeView1);
        this.TreeView1.ExpandAll();
    }

    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }
    /// <summary> 
    /// 创建一个树 
    /// </summary> 
    /// <param name="parentID">父ID</param> 
    /// <param name="node">节点</param> 
    /// <param name="dt">DataTable</param> 
    /// <param name="treeView">TreeView的名称</param> 
    public void CreateTree(String parentID, TreeNode node, DataTable dt, TreeView treeView)
    {
        //实例化一个DataView dt = 传入的DataTable 
        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [parentid]='" + parentID + "'";

        string template = "[{0}] |{1} | {2} | {3}";
        string ljScore = "";
        string specialScore = "";
        //用foreach遍历dv 
        foreach (DataRowView row in dv)
        {

            //第一次加载时为空 
            if (node == null)
            {
                //创建根节点 
                TreeNode root = new TreeNode();
                //必须与数据库的对应 
                ljScore = row["LjScore"].ToString();
                ljScore = string.IsNullOrEmpty(ljScore) ? "0.00" : ljScore;

                specialScore = row["SpecialScore"].ToString();
                specialScore = string.IsNullOrEmpty(specialScore) ? "0.00" : specialScore;

                root.Text = string.Format(template, row["userid"].ToString(), row["username"].ToString(), ljScore, specialScore);
                root.Value = row["userid"].ToString();
                //添加根节点 
                this.TreeView1.Nodes.Add(root);
                //递归调用方法本身 
                CreateTree(row["userid"].ToString(), root, dt, treeView);


            }
            else
            {
                //添加子节点 
                TreeNode childNode = new TreeNode();
                ljScore = row["LjScore"].ToString();
                ljScore = string.IsNullOrEmpty(ljScore) ? "0.00" : ljScore;

                specialScore = row["SpecialScore"].ToString();
                specialScore = string.IsNullOrEmpty(specialScore) ? "0.00" : specialScore;

                childNode.Text = childNode.Text = string.Format(template, row["userid"].ToString(), row["username"].ToString(), ljScore, specialScore);
                childNode.Value = row["userid"].ToString();
                node.ChildNodes.Add(childNode);
                CreateTree(row["userid"].ToString(), childNode, dt, treeView);

            }
        }

    }
}